from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
from app01 import models
from app01.utils.form import OrderModelForm
from app01.utils.pagination import Pagination
from django.http import JsonResponse
from app01.utils.datetime import rad_Datetime
from django.core import serializers

"""柱状图"""
def chart_list(request):
    return  render(request,'chart_list.html')

def chart_bar(request):
    """构造柱状图数据"""
    order_title_all=models.Order.objects.filter().values("title")
    order_price_all = models.Order.objects.filter().values("price")

    tietle_list=[]
    price_list =[]
    for i in order_title_all:
        tietle_list.append(i["title"][0:2])
    for i in order_price_all:
        price_list.append(i["price"])
    result={
        "status":True,
        "data":{
            'tietle_list':tietle_list,
            'price_list':price_list,
        }
    }
    return JsonResponse(result)

def chart_pie(request):
    """构造饼状图数据"""
    order_title_all = models.Order.objects.filter().values("title")
    order_price_all = models.Order.objects.filter().values("price")

    data = []
    j = 0
    for i in order_title_all:
        data_dact={
            'value': order_price_all[j]["price"],
            'name':order_title_all[j]["title"],

        }
        data.append(data_dact)
        j += 1
    result = {
        "status": True,
        "data":data,
    }

    return JsonResponse(result)

def chart_line(request):
    """构造折线图"""
    order_title_all = models.Order.objects.filter().values("title")
    order_price_all = models.Order.objects.filter().values("price")
    order_title_list=[]
    order_price_list=[]

    for i in order_title_all:
        order_title_list.append(i["title"][0:2])



    for i in order_price_all:
        order_price_list.append(i["price"])

    result = {
        "status": True,
        "xAxis": order_title_list,
        "series":order_price_list,
    }

    return JsonResponse(result)